package com.tth.learn.addproperties.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * 实现TCP网络编程
 *
 * 例题2.客户端发送文件给服务端，服务端将文件保存到本地
 * @Author tth
 * @Date 2022/12/28 15:02
 */
public class TCPTest2 {

    /**
     * 客户端
     */
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        try {
            String serverIp = "127.0.0.1";
            int serverPort = 9900;

            socket = new Socket(InetAddress.getByName(serverIp), serverPort);

            os = socket.getOutputStream();

            bis = new BufferedInputStream(new FileInputStream("big.png"));
            byte[] buff = new byte[1024];
            int len;
            while ((len = bis.read(buff)) != -1) {
                os.write(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 服务端
     */
    @Test
    public void server() {
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            ss = new ServerSocket(9900);

            socket = ss.accept();

            is = socket.getInputStream();
            Date date = new Date();
            bos = new BufferedOutputStream(new FileOutputStream(date.getTime()+".png"));
            byte[] buff = new byte[1024];
            int len;
            while ((len = is.read(buff)) != -1) {
                bos.write(buff, 0, len);
            }

            System.out.println("接收到来自"+socket.getInetAddress().getHostName()+"发送的图片");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
